package com.xjgzinfo.usdp.web.rest;

import com.xjgzinfo.usdp.core.domain.PageWapper;
import com.xjgzinfo.usdp.core.domain.ResponsePage;
import com.xjgzinfo.usdp.core.domain.ResponseResult;
import com.xjgzinfo.usdp.service.BladeVisualGlobalQueryService;
import com.xjgzinfo.usdp.service.BladeVisualGlobalService;
import com.xjgzinfo.usdp.service.dto.BladeVisualGlobalCriteria;
import com.xjgzinfo.usdp.service.dto.BladeVisualGlobalDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing {@link com.xjgzinfo.usdp.domain.BladeVisualGlobal}.
 */
@RestController
@RequestMapping("/blade-visual")
public class GlobalResource {

    private static final String ENTITY_NAME = "AvueServer4jAppBladeVisualGlobal";
    private final Logger log = LoggerFactory.getLogger(GlobalResource.class);
    private final BladeVisualGlobalService bladeVisualGlobalService;
    private final BladeVisualGlobalQueryService bladeVisualGlobalQueryService;
    @Value("${spring.application.name:usdp-app-unkown}")
    private String applicationName;

    public GlobalResource(BladeVisualGlobalService bladeVisualGlobalService, BladeVisualGlobalQueryService bladeVisualGlobalQueryService) {
        this.bladeVisualGlobalService = bladeVisualGlobalService;
        this.bladeVisualGlobalQueryService = bladeVisualGlobalQueryService;
    }

    /**
     * {@code POST  /visual-global/save} : 创建实体 bladeVisualGlobal.
     *
     * @param bladeVisualGlobalDTO the bladeVisualGlobalDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new bladeVisualGlobalDTO, or with status {@code 400 (Bad Request)} if the bladeVisualGlobal has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/visual-global/save")
    public ResponseResult<BladeVisualGlobalDTO> createBladeVisualGlobal(@Valid @RequestBody BladeVisualGlobalDTO bladeVisualGlobalDTO) throws URISyntaxException {
        log.debug("REST request to save BladeVisualGlobal : {}", bladeVisualGlobalDTO);
        if (bladeVisualGlobalDTO.getId() != null) {
            return new ResponseResult(400, null, "ID已存在", false);
        }
        BladeVisualGlobalDTO result = bladeVisualGlobalService.save(bladeVisualGlobalDTO);

        if (result.getId() != null) {
            return new ResponseResult(200, result, "创建成功", true);
        } else {
            return new ResponseResult(400, result, "创建失败", false);
        }
    }

    /**
     * {@code PUT  /visual-global/update} : 修改实体 bladeVisualGlobal.
     *
     * @param bladeVisualGlobalDTO the bladeVisualGlobalDTO to update.
     * @return the {@link ResponseResult} with status {@code 200 (OK)} and with body the updated bladeVisualGlobalDTO,
     * or with status {@code 400 (Bad Request)} if the bladeVisualGlobalDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the bladeVisualGlobalDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/visual-global/update")
    public ResponseResult<BladeVisualGlobalDTO> updateBladeVisualGlobal(@Valid @RequestBody BladeVisualGlobalDTO bladeVisualGlobalDTO) throws URISyntaxException {
        log.debug("REST request to update BladeVisualGlobal : {}", bladeVisualGlobalDTO);
        if (bladeVisualGlobalDTO.getId() == null) {
            return new ResponseResult(400, null, "ID不能为空", false);
        }
        BladeVisualGlobalDTO result = bladeVisualGlobalService.save(bladeVisualGlobalDTO);
        return new ResponseResult(200, result, "修改成功", true);
    }

    /**
     * {@code GET  /visual-global} : 获取全部实体 bladeVisualCategories.
     *
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of bladeVisualCategories in body.
     */
    @GetMapping("/visual-global")
    public ResponseResult<List<BladeVisualGlobalDTO>> getPageBladeVisualCategories(BladeVisualGlobalCriteria criteria) {
        log.debug("REST request to get BladeVisualCategories by criteria: {}", criteria);
        List<BladeVisualGlobalDTO> list = bladeVisualGlobalQueryService.findByCriteria(criteria);
        return new ResponseResult(200, list, "", true);
    }

    /**
     * {@code GET  /visual-global/list} : 分页获取实体列表 bladeVisualCategories.
     *
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of bladeVisualCategories in body.
     */
    @GetMapping("/visual-global/list")
    public ResponsePage<BladeVisualGlobalDTO> getAllBladeVisualCategories(BladeVisualGlobalCriteria criteria, Pageable pageable) {
        log.debug("REST request to get BladeVisualCategories by criteria: {}", criteria);
        Page<BladeVisualGlobalDTO> page = bladeVisualGlobalQueryService.findByCriteria(criteria, pageable);
        PageWapper<BladeVisualGlobalDTO> pageWapper = new PageWapper(page.getNumber(), page.getTotalPages(), page.getContent(), page.getSize(), page.getTotalElements());
        return new ResponsePage(200, pageWapper, "", true);
    }

    /**
     * {@code GET  /visual-global/count} : count all the bladeVisualCategories.
     *
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the count in body.
     */
    @GetMapping("/visual-global/count")
    public ResponseEntity<Long> countBladeVisualCategories(BladeVisualGlobalCriteria criteria) {
        log.debug("REST request to count BladeVisualCategories by criteria: {}", criteria);
        return ResponseEntity.ok().body(bladeVisualGlobalQueryService.countByCriteria(criteria));
    }

    /**
     * {@code GET  /visual-global/detail} : ID查询实体 bladeVisualGlobal.
     *
     * @param id the id of the bladeVisualGlobalDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the bladeVisualGlobalDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/visual-global/detail")
    public ResponseResult<BladeVisualGlobalDTO> getBladeVisualGlobal(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "id.equals", required = false) Long equalsId) {
        long queryId = id != null ? id : equalsId;
        log.debug("REST request to get BladeVisualGlobal : {}", queryId);
        Optional<BladeVisualGlobalDTO> bladeVisualGlobalDTO = bladeVisualGlobalService.findOne(queryId);
        if (bladeVisualGlobalDTO.isPresent()) {
            return new ResponseResult(200, bladeVisualGlobalDTO.get(), "", true);
        } else {
            return new ResponseResult(404, null, "未查询到数据", false);
        }
    }

    /**
     * {@code DELETE  /category/remove} : ID删除实体 bladeVisualGlobal.
     *
     * @param id the id of the bladeVisualGlobalDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @PostMapping("/visual-global/remove")
    public ResponseResult<Void> deleteBladeVisualGlobal(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "id.equals", required = false) Long equalsId) {
        long deleteId = id != null ? id : equalsId;
        log.debug("REST request to delete BladeVisualGlobal : {}", deleteId);
        bladeVisualGlobalService.delete(deleteId);
        return new ResponseResult(200, null, "删除成功", true);
//        Optional<BladeVisualGlobalDTO> bladeVisualGlobalDTOOpt = bladeVisualGlobalService.findOne(deleteId);
//        if (bladeVisualGlobalDTOOpt.isPresent()) {
//            BladeVisualGlobalDTO bladeVisualGlobalDTO = bladeVisualGlobalDTOOpt.get();
//            if (bladeVisualGlobalDTO.getIsDeleted() == 0) {
//                bladeVisualGlobalDTO.setIsDeleted(1);
//                bladeVisualGlobalService.save(bladeVisualGlobalDTO);
//            } else {
//                bladeVisualGlobalService.delete(deleteId);
//            }
//            return new ResponseResult(200, null, "删除成功", true);
//        } else {
//            return new ResponseResult(404, null, "未查询到数据", false);
//        }
    }
}
